﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Library.AppWeb;
using MPR.Poker.DALEntity.SYS;
using MPR.Poker.Model.SYS;
using PokerLife.Class;

namespace FlexBMS.Class
{
    public class FormatGrid
    {
        private static List<SYS_DisplayConfig> GetColumnDisplay(string TableName)
        {
            DAL_DisplayConfig db = new DAL_DisplayConfig();
            List<SYS_DisplayConfig> lst = new List<SYS_DisplayConfig>();

            ResultMessage result = db.SelectDisplayConfigList(ref lst, TableName, 1);

            return lst;
        }
        
        public static bool User(Kendo.Mvc.UI.Fluent.GridColumnFactory<PokerLife.Models.SYS.UserViewModel> columns)
        {
            List<SYS_DisplayConfig> lst = new List<SYS_DisplayConfig>();
            lst = GetColumnDisplay("SYS_User");
            List<string> lstpro = new PokerLife.Models.SYS.UserViewModel().GetType().GetProperties().Select(p => p.Name).ToList();
            lstpro = lstpro.Except(lst.Select(p => p.ColumnName)).ToList();

            foreach (string str in lstpro)
            {
                SYS_DisplayConfig obj = new SYS_DisplayConfig();
                obj.ColumnName = str.Trim();
                obj.DisplayName = str.Trim();
                obj.IsVisible = true;

                lst.Add(obj);
            }
                                    
            columns.Bound("")
                .HtmlAttributes(new { style = "text-align:center" })
                .HeaderTemplate("<input type='checkbox' id='ckSelectAll' class='ckSelectAll'  ")
                .HeaderHtmlAttributes(new { style = "text-align:center" })
                .Width(40)
                .ClientTemplate("<input style='text-align:center' type='checkbox' id='chkbx' class='chkbx' />");


            foreach (MPR.Poker.Model.SYS.SYS_DisplayConfig config in lst)
            {

                if (config.IsVisible)
                {
                    if (config.ColumnType != null && config.ColumnType.Contains(ColumnType.CheckBox.ToString()))
                    {
                        columns.Bound(config.ColumnName)

                                .Visible(true)
                                .Title(config.DisplayName)
                                .HeaderHtmlAttributes(new { style = "text-align:" + config.HeaderAlign })
                                .HtmlAttributes(new { style = "text-align:" + config.DataAlign + ";color:\\" + config.TextColor })
                                .Width((config.ColumnWidth == 0) ? "" : config.ColumnWidth + "px").Format(string.IsNullOrWhiteSpace(config.DataFormat) ? "" : ("{0:" + config.DataFormat + "}"))
                                 .ClientTemplate("<input disabled='disabled' id='" + config.ColumnName.Trim() + "' type='checkbox' #=" + config.ColumnName.Trim() + " ? checked='checked' : '' # />").Width(config.ColumnWidth);

                        continue;
                    }

                    if (config.ColumnType != null && config.ColumnType.Contains(ColumnType.Link.ToString()))
                    {
                        columns.Bound(config.ColumnName)
                                .Visible(true)
                                .Title(config.DisplayName)
                                .HeaderHtmlAttributes(new { style = "text-align:" + config.HeaderAlign })
                                .HtmlAttributes(new { style = "text-align:" + config.DataAlign + ";color:\\" + config.TextColor })
                                .Width((config.ColumnWidth == 0) ? "" : config.ColumnWidth + "px").Format(string.IsNullOrWhiteSpace(config.DataFormat) ? "" : ("{0:" + config.DataFormat + "}"))
                                .ClientTemplate("<a  href='\\#' class='Click_Popup' >#:" + config.ColumnName.Trim() + "#</a>");
                                //.ClientTemplate("<a  href='\\#' class='Click_Popup' onclick='Popup_Editor(this)' >#:" + config.ColumnName.Trim() + "#</a>");

                    }
                    else
                    {
                        columns.Bound(config.ColumnName)
                                .Visible(true)
                                .Title(config.DisplayName)
                                .HeaderHtmlAttributes(new { style = "text-align:" + config.HeaderAlign })
                                .HtmlAttributes(new { style = "text-align:" + config.DataAlign + ";color:\\" + config.TextColor })
                                .Width((config.ColumnWidth == 0) ? "" : config.ColumnWidth + "px").Format(string.IsNullOrWhiteSpace(config.DataFormat) ? "" : ("{0:" + config.DataFormat + "}"));
                    }
                }
            }
            return true;
        }

        public static bool Country(Kendo.Mvc.UI.Fluent.GridColumnFactory<PokerLife.Models.LIS.CountryViewModel> columns)
        {
            List<SYS_DisplayConfig> lst = new List<SYS_DisplayConfig>();
            lst = GetColumnDisplay("LIS_Country");
            List<string> lstpro = new PokerLife.Models.LIS.CountryViewModel().GetType().GetProperties().Select(p => p.Name).ToList();
            lstpro = lstpro.Except(lst.Select(p => p.ColumnName)).ToList();

            foreach (string str in lstpro)
            {
                SYS_DisplayConfig obj = new SYS_DisplayConfig();
                obj.ColumnName = str.Trim();
                obj.DisplayName = str.Trim();
                obj.IsVisible = true;

                lst.Add(obj);
            }

            columns.Bound("")
                .HtmlAttributes(new { style = "text-align:center" })
                .HeaderTemplate("<input type='checkbox' id='ckSelectAll' class='ckSelectAll'  ")
                .HeaderHtmlAttributes(new { style = "text-align:center" })
                .Width(40)
                .ClientTemplate("<input style='text-align:center' type='checkbox' id='chkbx' class='chkbx' />");


            foreach (MPR.Poker.Model.SYS.SYS_DisplayConfig config in lst)
            {

                if (config.IsVisible)
                {
                    if (config.ColumnType != null && config.ColumnType.Contains(ColumnType.CheckBox.ToString()))
                    {
                        columns.Bound(config.ColumnName)

                                .Visible(true)
                                .Title(config.DisplayName)
                                .HeaderHtmlAttributes(new { style = "text-align:" + config.HeaderAlign })
                                .HtmlAttributes(new { style = "text-align:" + config.DataAlign + ";color:\\" + config.TextColor })
                                .Width((config.ColumnWidth == 0) ? "" : config.ColumnWidth + "px").Format(string.IsNullOrWhiteSpace(config.DataFormat) ? "" : ("{0:" + config.DataFormat + "}"))
                                 .ClientTemplate("<input disabled='disabled' id='" + config.ColumnName.Trim() + "' type='checkbox' #=" + config.ColumnName.Trim() + " ? checked='checked' : '' # />").Width(config.ColumnWidth);

                        continue;
                    }

                    if (config.ColumnType != null && config.ColumnType.Contains(ColumnType.Link.ToString()))
                    {
                        columns.Bound(config.ColumnName)
                                .Visible(true)
                                .Title(config.DisplayName)
                                .HeaderHtmlAttributes(new { style = "text-align:" + config.HeaderAlign })
                                .HtmlAttributes(new { style = "text-align:" + config.DataAlign + ";color:\\" + config.TextColor })
                                .Width((config.ColumnWidth == 0) ? "" : config.ColumnWidth + "px").Format(string.IsNullOrWhiteSpace(config.DataFormat) ? "" : ("{0:" + config.DataFormat + "}"))
                                .ClientTemplate("<a  href='\\#' class='Click_Popup' >#:" + config.ColumnName.Trim() + "#</a>");
                        //.ClientTemplate("<a  href='\\#' class='Click_Popup' onclick='Popup_Editor(this)' >#:" + config.ColumnName.Trim() + "#</a>");

                    }
                    else
                    {
                        columns.Bound(config.ColumnName)
                                .Visible(true)
                                .Title(config.DisplayName)
                                .HeaderHtmlAttributes(new { style = "text-align:" + config.HeaderAlign })
                                .HtmlAttributes(new { style = "text-align:" + config.DataAlign + ";color:\\" + config.TextColor })
                                .Width((config.ColumnWidth == 0) ? "" : config.ColumnWidth + "px").Format(string.IsNullOrWhiteSpace(config.DataFormat) ? "" : ("{0:" + config.DataFormat + "}"));
                    }
                }
            }
            return true;
        }

    }

    /// <summary>
    /// Phương thức tổng hợp
    /// </summary>
    public enum Method
    {
        Count,
        Sum
    }
}